import type { PageType } from '../../../lib/page_handler/type'
import type { ModulePlatformConfig } from '../types'
import { BaiduPageType } from '../../../lib/page_handler/platform/baidu/types'
import { BianPageType } from '../../../lib/page_handler/platform/bian/types'
import { BingPageType } from '../../../lib/page_handler/platform/bing/types'
import { GiteePageType } from '../../../lib/page_handler/platform/gitee/types'
import { GithubPageType } from '../../../lib/page_handler/platform/github/types'
import { GooglePageType } from '../../../lib/page_handler/platform/google/types'
import { GreasyForkPageType } from '../../../lib/page_handler/platform/greasyfork/types'
import { IconfontPageType } from '../../../lib/page_handler/platform/iconfont/types'
import { SoPageType } from '../../../lib/page_handler/platform/so/types'
import { SogouPageType } from '../../../lib/page_handler/platform/sogou/types'
import { ZhiHuQuestionPageType } from '../../../lib/page_handler/platform/zhi_hu_question/types'
import { SUPPORTED_PLATFORMS } from '../../../types/constants'
import { EffectType, SYMBOLS } from '../../../types/interfaces'
import { AppModule } from '../types'

export function getInteractionModules(pageType: PageType): ModulePlatformConfig {
  return {
    [AppModule.TocEngine.id]: {
      meta: {
        name: '文章目录',
        description: '根据文章标题生成目录，支持拖动（默认在页面右上角位置）',
        category: 'appearance',
        icon: `${SYMBOLS.UI.LIST}${SYMBOLS.UI.NAVIGATION}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.CSDN]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.ZHI_HU]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.JIAN_SHU]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.JUE_JIN]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.FIVE_ONE_CTO]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.GITEE]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GiteePageType.Project,
              GiteePageType.HistoryProject,
            ]
            return supportPages.includes(pageType as GiteePageType)
          },
        },
      },
    },
    [AppModule.LinkDirect.id]: {
      meta: {
        name: '外链直达',
        description: '去除外链跳转确认，直接访问目标网址',
        category: 'interaction',
        icon: `${SYMBOLS.STATUS.LINK}${SYMBOLS.ARROWS.UP_RIGHT_EMOJI}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.MIXED,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.CSDN]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.ZHI_HU]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.JIAN_SHU]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.JUE_JIN]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.FIVE_ONE_CTO]: {
          status: 'supported',
        },
        [SUPPORTED_PLATFORMS.ZHI_HU_QUESTION]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              ZhiHuQuestionPageType.Question,
              ZhiHuQuestionPageType.Answer,
            ]
            return supportPages.includes(pageType as ZhiHuQuestionPageType)
          },
        },
        [SUPPORTED_PLATFORMS.GITEE]: {
          status: 'supported',
        },
      },
    },
    [AppModule.BlockLogin.id]: {
      meta: {
        name: '拦截登录',
        description: '拦截登录弹窗',
        category: 'interaction',
        icon: `${SYMBOLS.USER.USER_CENTER}${SYMBOLS.UI.BLOCK_ALERT}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.MIXED,
      },
    },
    [AppModule.QuickScroll.id]: {
      meta: {
        name: '快捷滚动',
        description: '一键滚动，页面加载时自动检测页面长度并智能显示（固定于页面右下角位置）',
        category: 'interaction',
        icon: `${SYMBOLS.WEATHER.LIGHTNING}${SYMBOLS.ARROWS.ARROW_UP_DOWN}`,
        status: 'supported',
        defaultEnabled: false,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        // TODO: 百度搜索存在元素隐藏后不需要滚动，但是快捷滚动还是会显示（即未实时动态检测）
        [SUPPORTED_PLATFORMS.BAI_DU]: {
          defaultEnabled: false,
          pageTypeValidator: () => {
            const notSupportPages = [
              BaiduPageType.Index,
              BaiduPageType.Advanced,
              BaiduPageType.NotFound,
            ]
            return !notSupportPages.includes(pageType as BaiduPageType)
          },
        },
        [SUPPORTED_PLATFORMS.ZHI_HU_QUESTION]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              ZhiHuQuestionPageType.Question,
              ZhiHuQuestionPageType.Answer,
            ]
            return supportPages.includes(pageType as ZhiHuQuestionPageType)
          },
        },
        [SUPPORTED_PLATFORMS.ICONFONT]: {
          status: 'not-needed',
        },
        [SUPPORTED_PLATFORMS.BIAN]: {
          status: 'supported',
        },
      },
    },
    [AppModule.StealthSearch.id]: {
      meta: {
        name: '无痕搜索',
        description: '关闭搜索自动完成和历史记录功能',
        category: 'interaction',
        icon: `${SYMBOLS.UI.SEARCH}${SYMBOLS.USER.GUEST}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.GREASY_FORK]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GreasyForkPageType.INDEX,
              GreasyForkPageType.SCRIPT_LIST,
              GreasyForkPageType.USER_PROFILE,
              GreasyForkPageType.SEARCH,
              GreasyForkPageType.USERS,
              GreasyForkPageType.BY_SITE,
              GreasyForkPageType.LIBRARIES,
              GreasyForkPageType.DISCUSSIONS,
              GreasyForkPageType.DISCUSSIONS_GREASY_FORK,
              GreasyForkPageType.DISCUSSIONS_DEVELOPMENT,
              GreasyForkPageType.DISCUSSIONS_REQUESTS,
              GreasyForkPageType.DISCUSSIONS_SCRIPT,
              GreasyForkPageType.DISCUSSIONS_NO_SCRIPT,
            ]
            return supportPages.includes(pageType as GreasyForkPageType)
          },
        },
        [SUPPORTED_PLATFORMS.BAI_DU]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              BaiduPageType.Advanced,
            ]
            return supportPages.includes(pageType as BaiduPageType)
          },
        },
      },
    },
    [AppModule.NewTabLinks.id]: {
      meta: {
        name: '新标签页',
        description: '强制链接在新标签页中打开',
        category: 'interaction',
        icon: `${SYMBOLS.NETWORK.GLOBE}${SYMBOLS.ARROWS.UP_RIGHT_EMOJI}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.MIXED,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.GREASY_FORK]: {
          status: 'supported',
          usePageTypeIsolation: true,
          description: () => {
            switch (pageType as GreasyForkPageType) {
              case GreasyForkPageType.SCRIPT_LIST:
                return '点击脚本列表中的脚本标题或作者后，会在新标签页打开关联页面'
              case GreasyForkPageType.SCRIPT_FEEDBACK:
                return '点击反馈列表中的标题后，会在新标签页打开关联页面'
              case GreasyForkPageType.DISCUSSIONS:
              case GreasyForkPageType.DISCUSSIONS_GREASY_FORK:
              case GreasyForkPageType.DISCUSSIONS_DEVELOPMENT:
              case GreasyForkPageType.DISCUSSIONS_REQUESTS:
              case GreasyForkPageType.DISCUSSIONS_SCRIPT:
              case GreasyForkPageType.DISCUSSIONS_NO_SCRIPT:
                return '点击论坛列表中的标题后，会在新标签页打开关联页面'
              case GreasyForkPageType.SEARCH:
                return '点击搜索按钮后，会在新标签页打开关联页面'
              case GreasyForkPageType.MODERATOR_ACTIONS:
                return '点击表格中的链接后，会在新标签页打开关联页面'
              case GreasyForkPageType.USER_PROFILE:
                return '点击控制台链接后，会在新标签页打开关联页面'
              default:
                return '强制链接在新标签页中打开'
            }
          },
          pageTypeValidator: () => {
            const supportPages = [
              GreasyForkPageType.INDEX,
              GreasyForkPageType.SCRIPT_LIST,
              GreasyForkPageType.SCRIPT_INFO,
              GreasyForkPageType.SCRIPT_FEEDBACK,
              GreasyForkPageType.DISCUSSIONS,
              GreasyForkPageType.DISCUSSIONS_GREASY_FORK,
              GreasyForkPageType.DISCUSSIONS_DEVELOPMENT,
              GreasyForkPageType.DISCUSSIONS_REQUESTS,
              GreasyForkPageType.DISCUSSIONS_SCRIPT,
              GreasyForkPageType.DISCUSSIONS_NO_SCRIPT,
              GreasyForkPageType.SEARCH,
              GreasyForkPageType.MODERATOR_ACTIONS,
              GreasyForkPageType.REPORTS,
              GreasyForkPageType.HELP,
              GreasyForkPageType.BY_SITE,
              GreasyForkPageType.USER_PROFILE,
            ]
            return supportPages.includes(pageType as GreasyForkPageType)
          },
        },
        [SUPPORTED_PLATFORMS.GITHUB]: {
          status: 'supported',
          description: '点击 README 文档的链接后，会在新标签页打开关联页面',
          pageTypeValidator: () => {
            const supportPages = [
              GithubPageType.Project,
            ]
            return supportPages.includes(pageType as GithubPageType)
          },
        },
      },
    },
    [AppModule.AutoPagination.id]: {
      meta: {
        name: '自动分页',
        description: '滚动到底部自动加载内容，失效时可手动点击加载；加载新内容后解禁滚动加载',
        category: 'interaction',
        icon: `${SYMBOLS.DOCUMENTS.PAGE}${SYMBOLS.OTHERS.NEXT}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.BAI_DU]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              BaiduPageType.Web,
              BaiduPageType.News,
            ]
            return supportPages.includes(pageType as BaiduPageType)
          },
        },
        [SUPPORTED_PLATFORMS.GREASY_FORK]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GreasyForkPageType.SCRIPT_LIST,
              GreasyForkPageType.SCRIPT_VERSIONS,
              GreasyForkPageType.SCRIPT_FEEDBACK,
            ]
            return supportPages.includes(pageType as GreasyForkPageType)
          },
        },
        [SUPPORTED_PLATFORMS.BIAN]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              BianPageType.List,
            ]
            return supportPages.includes(pageType as BianPageType)
          },
        },
        [SUPPORTED_PLATFORMS.GITEE]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GiteePageType.Commits,
            ]
            return supportPages.includes(pageType as GiteePageType)
          },
        },
        [SUPPORTED_PLATFORMS.GITHUB]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GithubPageType.Tags,
            ]
            return supportPages.includes(pageType as GithubPageType)
          },
        },
      },
    },
    [AppModule.MarkRead.id]: {
      meta: {
        name: '标记已读',
        description: '标记已读内容',
        category: 'interaction',
        icon: `${SYMBOLS.DOCUMENTS.DOCUMENT_WITH_PAGE}${SYMBOLS.OTHERS.VIEWED}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.BAI_DU]: {
          status: 'supported',
          description: '搜索结果标题被点击后，会被标记为已读',
          pageTypeValidator: () => {
            const supportPages = [
              BaiduPageType.Web,
              BaiduPageType.News,
            ]
            return supportPages.includes(pageType as BaiduPageType)
          },
        },
      },
    },
    [AppModule.SuperSearch.id]: {
      meta: {
        name: '超级搜索',
        description: '一键应用高级搜索语法，精准定位搜索结果（点击按钮位于搜索框内部）',
        category: 'interaction',
        icon: `${SYMBOLS.UI.SEARCH}${SYMBOLS.UI.GEM}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.BAI_DU]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              BaiduPageType.Web,
            ]
            return supportPages.includes(pageType as BaiduPageType)
          },
        },
      },
    },
    [AppModule.SvgCopy.id]: {
      meta: {
        name: 'SVG 复制',
        description: '复制图标 SVG 代码，清理 p-id 等冗余属性，优化为单引号、自闭合标签格式',
        category: 'interaction',
        icon: `${SYMBOLS.FILES.IMAGE}${SYMBOLS.STATUS.SPARKLES}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.ICONFONT]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              IconfontPageType.SearchIcon,
              IconfontPageType.CollectionsDetail,
            ]
            return supportPages.includes(pageType as IconfontPageType)
          },
        },
      },
    },
    [AppModule.KeyboardPagination.id]: {
      meta: {
        name: '键盘翻页',
        description: '使用 ←→ 方向键快速切换上一页/下一页',
        category: 'interaction',
        icon: `${SYMBOLS.UI.KEYBOARD}${SYMBOLS.ARROWS.LEFT_RIGHT}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.BIAN]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              BianPageType.List,
            ]
            return supportPages.includes(pageType as BianPageType)
          },
        },
        [SUPPORTED_PLATFORMS.SO_GOU]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              SogouPageType.Web,
              SogouPageType.Weixin,
              SogouPageType.Zhihu,
              SogouPageType.Video,
              SogouPageType.Medical,
              SogouPageType.Ask,
              SogouPageType.Encyclopedia,
              SogouPageType.News,
              SogouPageType.Knowledge,
            ]
            return supportPages.includes(pageType as SogouPageType)
          },
        },
        [SUPPORTED_PLATFORMS.GREASY_FORK]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GreasyForkPageType.SCRIPT_LIST,
              GreasyForkPageType.SCRIPT_VERSIONS,
              GreasyForkPageType.SCRIPT_FEEDBACK,
              GreasyForkPageType.DISCUSSIONS,
              GreasyForkPageType.DISCUSSIONS_GREASY_FORK,
              GreasyForkPageType.DISCUSSIONS_DEVELOPMENT,
              GreasyForkPageType.DISCUSSIONS_SCRIPT,
              GreasyForkPageType.DISCUSSIONS_NO_SCRIPT,
              GreasyForkPageType.USERS,
              GreasyForkPageType.MODERATOR_ACTIONS,
              GreasyForkPageType.LIBRARIES,
            ]
            return supportPages.includes(pageType as GreasyForkPageType)
          },
        },
        [SUPPORTED_PLATFORMS.GITEE]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GiteePageType.Commits,
            ]
            return supportPages.includes(pageType as GiteePageType)
          },
        },
        [SUPPORTED_PLATFORMS.GITHUB]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GithubPageType.Commits,
              GithubPageType.Tags,
            ]
            return supportPages.includes(pageType as GithubPageType)
          },
        },
      },
    },
    [AppModule.CleanSearchHistory.id]: {
      meta: {
        name: '清理历史',
        description: '在页面加载完成或功能重新启用时，自动触发搜索框点击并清除全部历史记录',
        category: 'interaction',
        icon: `${SYMBOLS.UI.SEARCH}${SYMBOLS.UI.TRASH}`,
        status: 'not-needed',
        defaultEnabled: false,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.GOOGLE]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GooglePageType.Index,
              GooglePageType.IndexHk,
              GooglePageType.All,
              GooglePageType.Video,
              GooglePageType.ShortVideo,
              GooglePageType.Web,
            ]
            return supportPages.includes(pageType as GooglePageType)
          },
        },
        [SUPPORTED_PLATFORMS.BING]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              BingPageType.Web,
              BingPageType.Video,
              BingPageType.Academic,
              BingPageType.Dictionary,
              BingPageType.Index,
            ]
            return supportPages.includes(pageType as BingPageType)
          },
        },
        [SUPPORTED_PLATFORMS.SO_GOU]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              SogouPageType.Web,
              SogouPageType.Zhihu,
              SogouPageType.Medical,
              SogouPageType.Ask,
              SogouPageType.Encyclopedia,
              SogouPageType.News,
              SogouPageType.Knowledge,
              SogouPageType.IndexSearch,
            ]
            return supportPages.includes(pageType as SogouPageType)
          },
        },
        [SUPPORTED_PLATFORMS.SO]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              SoPageType.Video,
              SoPageType.News,
              SoPageType.Wenku,
              SoPageType.IndexSearch,
              SoPageType.IndexVideo,
              SoPageType.IndexWenku,
            ]
            return supportPages.includes(pageType as SoPageType)
          },
        },
      },
    },
    [AppModule.ConversationFlow.id]: {
      meta: {
        name: '对话脉络',
        description: '可视化评论引用链和用户消息流',
        category: 'interaction',
        icon: `${SYMBOLS.OTHERS.COMMENT}${SYMBOLS.STATUS.LINK}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.GREASY_FORK]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GreasyForkPageType.SCRIPT_DISCUSSION,
              GreasyForkPageType.DISCUSSIONS_DETAIL,
            ]
            return supportPages.includes(pageType as GreasyForkPageType)
          },
        },
      },
    },
    [AppModule.QuickDownload.id]: {
      meta: {
        name: '快速下载',
        description: '悬浮显示下载按钮，并在点击后触发下载',
        category: 'interaction',
        icon: `${SYMBOLS.UI.MOUSE}${SYMBOLS.UI.DOWNLOAD}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.BIAN]: {
          status: 'supported',
          loginRequired: true,
          pageTypeValidator: () => {
            const supportPages = [
              BianPageType.List,
            ]
            return supportPages.includes(pageType as BianPageType)
          },
        },
      },
    },
    [AppModule.ImageDetail.id]: {
      meta: {
        name: '图片详情',
        description: '悬停图片时显示尺寸和大小信息',
        category: 'interaction',
        icon: `${SYMBOLS.UI.MOUSE}${SYMBOLS.STATUS.INFO}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.BIAN]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              BianPageType.List,
            ]
            return supportPages.includes(pageType as BianPageType)
          },
        },
      },
    },
    [AppModule.ImagePreview.id]: {
      meta: {
        name: '图片预览',
        description: '悬停图片文件时显示预览',
        category: 'interaction',
        icon: `${SYMBOLS.FILES.IMAGE}${SYMBOLS.OTHERS.VIEWED}`,
        status: 'not-needed',
        defaultEnabled: true,
        effectType: EffectType.IMMEDIATE,
      },
      supportedPlatforms: {
        [SUPPORTED_PLATFORMS.GITEE]: {
          status: 'supported',
          pageTypeValidator: () => {
            const supportPages = [
              GiteePageType.Project,
              GiteePageType.HistoryProject,
              GiteePageType.CodeView,
            ]
            return supportPages.includes(pageType as GiteePageType)
          },
        },
      },
    },
  }
}
